package com.jwb.pub.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/pub/prop")
public class PropController {

	/**
	 * 遍历项目下含有.pro 文件
	 * @param req
	 * @return  返回 文件名list    装入select 控件 
	 */
	@RequestMapping("filelist")
	@ResponseBody
	public String filelist(HttpServletRequest req){
		JSONObject jsobject = new JSONObject();
		JSONArray array = new JSONArray();
		String path = req.getServletContext().getRealPath("/")+"WEB-INF/classes";
		File dir = new File(path);
		if(dir.exists()&&dir.isDirectory()){
			for(File f:dir.listFiles()){
				if(f.getName().endsWith(".properties")){
					JSONObject jso = new JSONObject();
					jso.put("label", f.getName());
					jso.put("value", f.getPath());
					array.add(jso);
				}			
			}
			jsobject.put("list", array);
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		}else{
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "访问文件夹失败");
		}
	
		return jsobject.toString();
	}
	
	
	
	/**
	 * 
	 * 通过选择文件名参数 查找该文件下的 属性名和属性值 list
	 * @param req   fileName 文件名
	 * @return  返回 文件名list    装入select 控件 
	 */
	@RequestMapping("proplist")
	@ResponseBody
	public String proplist(HttpServletRequest req){
		JSONObject jsobject = new JSONObject();
		JSONArray list = new JSONArray();
		String path = req.getServletContext().getRealPath("/")+"WEB-INF/classes/";
		String fileName = req.getParameter("fileName") == null ?"":req.getParameter("fileName"); 
		Properties prop = new Properties();
		
		try (
			InputStream is = PropController.class.getClassLoader().getResourceAsStream(fileName);
		){
			prop.load(is);
			for(Object key: prop.keySet()){
				JSONObject jso = new JSONObject();
				jso.put("name", key.toString());
				jso.put("value", prop.get(key));
				list.add(jso);
			}			
			jsobject.put("list", list);
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
			is.close();
		} catch (IOException e) {
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "文件不存在");
		}
		return jsobject.toString();
	}
	
	
	
	/**
	 * 
	 * operType add ,edit addFile ,del
	 * add proName,proValue
	 * edit proName modify proValue
	 * del  proName 
	 * 
	 *  
	 */
	@RequestMapping("propSave")
	@ResponseBody
	public String propSave(HttpServletRequest req){
		JSONObject jsobject = new JSONObject();
		String fileName = req.getParameter("fileName") == null ?"":req.getParameter("fileName"); 
		String operType = req.getParameter("operType") == null ?"":req.getParameter("operType"); 
		String proName = req.getParameter("proName") == null ?"":req.getParameter("proName"); 
		String proValue = req.getParameter("proValue") == null ?"":req.getParameter("proValue");
		String path = req.getServletContext().getRealPath("/")+"WEB-INF/classes/";
		Properties prop = new Properties();
		FileInputStream in = null;
		try {
			in = new FileInputStream(path+fileName);
			prop.load(in);			
			FileOutputStream fos = new FileOutputStream(path+fileName);
			if(operType.equals("add")&&prop.containsKey(proName)){
				jsobject.put("respCode", "999");
				jsobject.put("respDesc", proName+"属性已存在");
			}else if(operType.equals("add")||operType.equals("edit")){
				prop.setProperty(proName, proValue);
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
			}else if(operType.equals("del")){
				prop.remove(proName);
				jsobject.put("respCode", "0");
				jsobject.put("respDesc", "OK");
			}	
			
			prop.store(fos, "");
			fos.close();
		} catch (Exception e) {
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "读取文件失败");
		}finally{
			try {
				in.close();
			} catch (IOException e) {
			}
		}
		
		return jsobject.toString();
	}
	
	
	
	/**
	 * 
	 * 删除文件
	 * @param req
	 * @return   
	 */
	@RequestMapping("fileDel")
	@ResponseBody
	public String fileDel(HttpServletRequest req){
		JSONObject jsobject = new JSONObject();
		String fileName = req.getParameter("fileName") == null ?"":req.getParameter("fileName"); 
		String path = req.getServletContext().getRealPath("/")+"WEB-INF/classes/";
		File f = new File(path+fileName);
		if(!f.exists()){
			jsobject.put("respCode", "-1");
			jsobject.put("respDesc", "文件不存在");
		}else{
			f.delete();
			jsobject.put("respCode", "0");
			jsobject.put("respDesc", "OK");
		}	
		
		return jsobject.toString();
	}
	
	
}
